package com.health.ts.utils;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.google.common.collect.Lists;
import com.health.ts.entity.sysmanage.SysUser;

/**
 * 用户信息工具类
 */
public class SessionUserUtil {

	public static Subject getSubject() {
		return SecurityUtils.getSubject();
	}

	public static Session getSession() {
		return getSubject().getSession();
	}

	public static SysUser getUser() {
		SysUser user = null;
		Object obj = getSubject().getPrincipal();
		if (obj != null) {
			user = (SysUser)obj;
		}
		return user;
	}
	
	/**
	 * 获取用户ID
	 * @return
	 */
	public static String getUserId(){
		return isNull(getUser().getId());
	}
	
	/**
	 * 获取用户名称
	 * @return
	 */
	public static String getUserName(){
		return isNull(getUser().getUser_name());
	}
	
	/**
	 * 获取用户code
	 * @return
	 */
	public static String getUserCode(){
		return isNull(getUser().getUser_code());
	}
	
	/**
	 * 获取用户account_name
	 * @return
	 */
	public static String getUserAccountName(){
		return isNull(getUser().getAccount_name());
	}
	
	/**
	 * 获取用户user_type
	 * @return
	 */
	public static String getUserType(){
		return isNull(getUser().getUser_type());
	}
	
	/**
	 * 获取用户isSuper
	 * @return
	 */
	public static boolean userIsSuper(){
		if("1".equals(isNull(getUser().getIsSuper()))){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断用户是否是系统管理员
	 * @return
	 */
	public static boolean userIsSysAdmin(){
		if("1".equals(getUserType())){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断用户是否是医院管理员
	 * @return
	 */
	public static boolean userIsHosAdmin(){
		if("2".equals(getUserType())){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断用户是否是业务用户
	 * @return
	 */
	public static boolean userIsBusinessUser(){
		if("3".equals(getUserType())){
			return true;
		}
		return false;
	}
	
	/**
	 * 获取用户hospital_id
	 * @return
	 */
	public static String getUserHosId(){
		return isNull(getUser().getHospital_id());
	}
	
	
	
	/**
	 * 获取用户department_id
	 * @return
	 */
	public static String getUserDeptId(){
		return isNull(getUser().getDepartment_id());
	}
	
	/**
	 * 获取用户role
	 * @return
	 */
	public static String getUserRoles(){
		return isNull(getUser().getRole_id());
	}
	
	/**
	 * 判断用户是否有指定角色
	 * @param roleIdentifier
	 * @return
	 */
	public static boolean hasRole(String roleIdentifier) {
		return getSubject().hasRole(roleIdentifier);
	}
	
	/**
	 * 判断用户是否有指定权限
	 * @param permission
	 * @return
	 */
	public static boolean isPermitted(String permission) {
		return getSubject().isPermitted(permission);
	}
	
	/**
	 * 获取用户role列表
	 * @return
	 */
	public static List<String> getUserRoleList(){
		String roles = getUserRoles();
		if(StringUtils.isNotEmpty(roles)) {
			return Lists.newArrayList(roles.split(","));
		}
		return Collections.emptyList();
	}
	
	/**
	 * 判断对象为空
	 */
	private static String isNull(Object obj){
		if(obj==null){
			return "";
		}else{
			return obj.toString();
		}
	}
}